from fastapi import APIRouter,Depends,HTTPException

from dependencies import get_token_header

router = APIRouter(
    prefix="/items",
    tags=["items"],
    dependencies=[Depends(get_token_header)],
    responses={404: {"description": "Not found"}},
)

items_db = [{"id":1,"name":"item1"},{"id":2,"name":"item2"}]

@router.get("/")
async def read_items():
    return items_db

@router.get("/{item_id}")
async def read_item(item_id: int):
    for item in items_db:
        if item["id"] == item_id:
            return item
    raise HTTPException(status_code=404, detail="没有找到您需要的item")

@router.put("/{item_id}",tags=["custom"], responses={403: {"description": "Operation forbidden"}})
async def update_item(item_id: int):
    if item_id != 2:
        raise HTTPException(status_code=403, detail="您没有权限更新此item")
    return {"item_id": item_id,"name": "item2"}
